package ai.people.netmon.telegram.service.impl;


import ai.people.netmon.framework.domain.searchmsg.request.MessageRequest;
import ai.people.netmon.framework.domain.telegram.entity.Channels;
import ai.people.netmon.framework.domain.telegram.entity.Topic;
import ai.people.netmon.framework.domain.telegram.entity.Users;
import ai.people.netmon.framework.domain.telegram.vo.channels.ChatMessage;
import ai.people.netmon.framework.domain.telegram.vo.topic.AnalyzedTopic;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.exception.enums.TopicExceptionEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.netmon.telegram.domain.request.AnalyzedListRequest;
import ai.people.netmon.telegram.domain.request.topic.RelatedMessageRequest;
import ai.people.netmon.telegram.domain.request.topic.RelatedTopicRequest;
import ai.people.netmon.telegram.mapper.TopicAnalysisMapper;
import ai.people.netmon.telegram.service.MessagesService;
import ai.people.netmon.telegram.service.TopicAnalysisService;
import ai.people.netmon.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 主题分析服务impl
 *
 * @author yuanqinglong
 * @date 2022/1/14 18:15
 */
@Service
public class TopicAnalysisServiceImpl implements TopicAnalysisService {

    @Autowired
    private TopicAnalysisMapper topicAnalysisMapper;

    @Autowired
    private MessagesService messagesService;

    /**
     * 获取主题
     *
     * @param topicId 主题id
     * @return {@link Topic}
     */
    @Override
    public Topic getTopic(Long topicId) {
        Topic topic = topicAnalysisMapper.selectById(topicId);
        AssertUtils.isNotNull(topic, TopicExceptionEnum.TOPIC_NOT_EXISTS);
        return topic;
    }


    /**
     * 添加主题
     *
     * @param topic 主题
     */
    @Override
    public void addTopic(Topic topic) {
        int insert = topicAnalysisMapper.insert(topic);
        AssertUtils.isTrue(insert > 0, CommonEnum.FAIL);
    }


    /**
     * 更新主题
     *
     * @param topic 主题
     */
    @Override
    public void updateTopic(Topic topic) {
        int updateTopic = topicAnalysisMapper.updateById(topic);
        AssertUtils.isTrue(updateTopic > 0, CommonEnum.FAIL);
    }

    /**
     * 相关群组
     *
     * @param relatedChannelsRequest 相关群组请求参数
     * @return {@link PageResponse}<{@link Channels}>
     */
    @Override
    public PageResponse<Channels> relatedChannels(RelatedTopicRequest relatedChannelsRequest) {
        long channelsCount = topicAnalysisMapper.relatedChannelsCount(relatedChannelsRequest);
        if (channelsCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<Channels> relatedChannels = topicAnalysisMapper.relatedChannels(relatedChannelsRequest);
        return PageResponse.builder(relatedChannels, channelsCount);
    }

    /**
     * 相关账户
     *
     * @param relatedTopicRequest 相关账号请求参数
     * @return {@link PageResponse}<{@link Users}>
     */
    @Override
    public PageResponse<Users> relatedAccount(RelatedTopicRequest relatedTopicRequest) {
        long accountCount = topicAnalysisMapper.relatedAccountCount(relatedTopicRequest);
        if (accountCount == 0) {
            return PageResponse.builderEmpty();
        }
        List<Users> relatedAccount = topicAnalysisMapper.relatedAccount(relatedTopicRequest);
        return PageResponse.builder(relatedAccount, accountCount);
    }

    /**
     * 相关消息
     *
     * @param relatedMessageRequest 相关消息请求
     * @return {@link PageResponse}<{@link ChatMessage}>
     */
    @Override
    public PageResponse<ChatMessage> relatedMessage(RelatedMessageRequest relatedMessageRequest) {
        MessageRequest messageRequest = relatedMessageRequest.createMessageRequest();
        // 默认查询主题配置的关键词
        if (StringUtils.isEmpty(messageRequest.getSearchKeywords())) {
            LambdaQueryWrapper<Topic> topicQueryWrapper = Wrappers.lambdaQuery();
            topicQueryWrapper.select(Topic::getKeywords).eq(Topic::getId, relatedMessageRequest.getTopicId());
            Topic topic = topicAnalysisMapper.selectOne(topicQueryWrapper);
            AssertUtils.isNotNull(topic, TopicExceptionEnum.TOPIC_NOT_EXISTS);
            messageRequest.setSearchKeywords(topic.getKeywords());
        }
        return messagesService.globalInfoSearch(messageRequest);
    }

    /**
     * 已加主题分析列表
     *
     * @param analyzedListRequest 参数
     * @return {@link List}<{@link AnalyzedTopic}>
     */
    @Override
    public List<AnalyzedTopic> analyzedTopics(AnalyzedListRequest analyzedListRequest) {
        return topicAnalysisMapper.analyzedTopics(analyzedListRequest);
    }

    /**
     * 删除主题
     *
     * @param topicId 主题id
     */
    @Override
    public void deleteTopic(Long topicId) {

    }
}
